<template>
  <su-page bg="#fff">
    <su-body>
      <!-- header -->
      <su-white-header v-if="detailData.BT" :title="detailData.BT" :prop="detailData.FSSJ"></su-white-header>
      <!-- detail -->
      <su-prop-group title="通知详细信息" v-if="detailData">
        <su-prop label="发送人">{{detailData.FSR || ''}}</su-prop>
        <su-prop label="发送单位">{{detailData.FSDW || ''}}</su-prop>
        <su-prop label="附件">
        </su-prop>
      </su-prop-group>
      <view class="file">
      	<view class="fileList" v-for="(item, index) in fileArr" :key="index">
      			<u--image radius="6" :src="item.previewUrl" width="70" height="70" mode="aspectFill" @click="previewImg(index)"></u--image>		
      	</view>
      </view>
			<view class="hfnr">
				<view  v-if="isReceipt === '1'">回复时间  {{fb_data.HFSJ ? fb_data.HFSJ : ''}}</view>
				<view>回复内容</view>
				<u--textarea v-model="hfnr" placeholder="请输入回复内容"  v-if="isReceipt === '0'"></u--textarea>
				<u--textarea v-else disabled v-model="fb_data.HFNR" autoHeight ></u--textarea>
			</view>
			<view class="file">
				<view class="fileList" v-for="(item, index) in fileArr_2" :key="index">
						<u--image radius="6" :src="item.previewUrl" width="70" height="70" mode="aspectFill" @click="previewImg(index)"></u--image>		
				</view>
			</view>
			<view class="su-uploader" v-if="isReceipt === '0'">
				<view class="su-uploader-handler" @click="chooseWxFile">
				  +
				</view>
				<view class="su-uploader-images-item" v-for="(item,index) in fjArr" :key="index">
				  <view class="su-uploader-images-delete" @click="deleteItem(item,index)">
				    <u-icon name="close" color="#ff0000" size="18"></u-icon>
				  </view>
				  <u--image radius="6" :src="item.imgurl" width="70" height="70" mode="aspectFill" @click="previewImg2(index)"></u--image>
				</view>
			</view>
			<su-fixed-footer transparent v-if="isReceipt === '0'">
			  <u-button type="primary" size="large" :loading="loading" text="提交" @click="submitForm"></u-button>
			</su-fixed-footer>
      <!-- loading -->
      <su-list-loading :loading="detailLoading"></su-list-loading>
    </su-body>
		<u-toast ref="uToast"></u-toast>
  </su-page>
</template>

<script>
	/* @Description: 企业账号，回执详情
	 * @Author: Yinchy
	 * @CreatDate: 2022-11-23 20:42:15
	 * @Params: 
	 */
	import { runSQL,uploadFilePath,fileIcon, previewPath } from '@/common/utils/wxutils'

  export default {
    data() {
      return {
				isReceipt: "0",
				fb_data: null,
				loading: false,
        recid:'',
        detailData: null,
        detailLoading: false,
				fileArr: [],
				fileArr_2: [],
				hfnr: "",
				fjArr: [],
				fileStr: ""
      }
    },
    onLoad(params) {
      this.detailData = params || null;
			this.isReceipt = params.curNow || "0";
      this.recid = params.RECID;
      this.getDetail()
    },
    methods: {
      // 获取详情
      getDetail() {
        this.detailLoading = true;
        let fileidArr = this.detailData.FJ ? this.detailData.FJ.split(',') : [this.detailData.FJ];
        fileidArr.forEach(item => {
        	runSQL(`select * from ssnydw.tbfile where fileid = '${item}';`).then(res => {
        		if(res[0].length > 0){
        			res[0].forEach(file=>{
        				if(!(/png|jpg|jpeg/).test(file.FILENAME)){
        					file.previewUrl = fileIcon
        				}else{
        					file.previewUrl = previewPath + file.FILEID
        				}
        			})
        			this.fileArr  = this.fileArr.concat(res[0][0]);
        		}
        	})
        })
				runSQL(`select * from ssnydw.tzfsfb where XMJLID = ${JSON.parse(uni.getStorageSync('userInfo')).HUMANID} and recid = ${this.recid};`).then(async hz=>{
					if(hz[0].length > 0){
						this.fb_data = hz[0][0];
						let fileidArrs = this.fb_data.HFFJ ? this.fb_data.HFFJ.split(',') : [this.fb_data.HFFJ];
						fileidArrs.forEach(item => {
							runSQL(`select * from ssnydw.tbfile where fileid = '${item}';`).then(res => {
								if(res[0].length > 0){
									res[0].forEach(file=>{
										if(!(/png|jpg|jpeg/).test(file.FILENAME)){
											file.previewUrl = fileIcon
										}else{
											file.previewUrl = previewPath + file.FILEID
										}
									})
									this.fileArr_2  = this.fileArr_2.concat(res[0][0]);
								}
							})
						})
					}
				})
        this.detailLoading = false;
      },
			chooseWxFile(){
				let _this = this;
				// #ifdef MP-WEIXIN
				wx.chooseMessageFile({
					count: 9,
					type: 'all',
					success: (res)=>{
						const tempFilePaths = res.tempFiles;
						tempFilePaths.forEach(file => {
							if(!(/png|jpg|jpeg/.test(file.path.toLocaleLowerCase()))){
								file.imgurl = fileIcon
							}else{
								file.imgurl = file.path
							}
						})
						_this.fjArr = _this.fjArr.concat(tempFilePaths)
					}
				})
				// #endif
			},
			async submitForm(){
				let _this = this;
				let fb_data = this.fb_data;
				for(let i=0;i<_this.fjArr.length;i++){
					let file = _this.fjArr[i];
					await _this.uploadFileAsync(_this.recid, file.path).then(f=>{
						_this.fileStr = _this.fileStr + f.fileid + ","
						runSQL(`insert into ssnydw.tbfile(fileid,filename,filepath,filesize,lastupdate,humanid,storefilename,serverid,folderid,disporder,version) values('${f.fileid}','${f.wjm}','${f.url}',100,now(),${JSON.parse(uni.getStorageSync('userInfo')).HUMANID},'${f.wjm}',1,'${fb_data.UUID}',1,1);`).then(i => {
							console.log(11111, i);
						});
					})
				}
				runSQL(`update ssnydw.tzfsfb set zt = '1',hfnr='${_this.hfnr}',HFFJ='${_this.fileStr.substring(0,_this.fileStr.length-1)}',hfsj=now() where uuid = '${fb_data.UUID}';`).then(u=>{
					console.log(222, u);
					if(u[0] == 1){
						_this.$refs.uToast.show({
							type: 'success',
							message: "已发送",
							complete(){
								uni.navigateBack({
									delta: 1
								})
							}
						})
					}else{
						_this.$refs.uToast.show({
							type: 'error',
							message: "发送失败"
						})
					}
				})
			},
			uploadFileAsync(recid, url) {
				let _this = this;
				return new Promise( async (resolve, reject)=>{
					await uni.uploadFile({
						url: uploadFilePath, // 仅为示例，非真实的接口地址`${zhhfurl}zhhf-info/WeChat/getOpenId`,
						filePath: url,
						name: 'file',
						formData: {
							recid: recid
						},
						success: (res) => {
							let data = JSON.parse(res.data);
							let reg =  new RegExp("\\/"+data.wjm);
							let url_n = data.url.replace(/D:\/\/SSNYDWFile/,'').replace(reg, '');
							data.url = url_n;
							resolve(data)
						}
					});
				})
			},
			deleteItem(item, index){
				this.fjArr.splice(index, 1)
			},
			previewImg(idnex){
				let _this = this;
				wx.downloadFile({
				  //文件url
				  url: previewPath + _this.fileArr[index].FILEID,
				  success: function (res) {
				    const filePath = res.tempFilePath
				    wx.openDocument({
				      filePath: filePath,
							fileType: _this.fileArr[index].STOREFILENAME.substring(_this.fileArr[index].STOREFILENAME.lastIndexOf(".")+1),
				      success: function (res) {
				        console.log('打开文档成功')
				      },
				      fail: function (res) {
				        console.log(res,'打开文档失败')
				      },
				    })
				  }
				})
			},
			previewImg2(idnex){
				let _this = this;
				wx.downloadFile({
				  //文件url
				  url: previewPath + _this.fileArr_2[index].FILEID,
				  success: function (res) {
				    const filePath = res.tempFilePath
				    wx.openDocument({
				      filePath: filePath,
							fileType: _this.fileArr_2[index].STOREFILENAME.substring(_this.fileArr_2[index].STOREFILENAME.lastIndexOf(".")+1),
				      success: function (res) {
				        console.log('打开文档成功')
				      },
				      fail: function (res) {
				        console.log(res,'打开文档失败')
				      },
				    })
				  }
				})
			}
			
    },
  }
</script>

<style lang="scss" scoped>
	.file {
		display: flex;
		flex-wrap: wrap;
		background-color: #fff;
		padding: 20upx;
		margin-right: 20upx;
	}
	.hfnr {
		padding: 20upx;
	}
	.su-uploader{
	  display: flex;
	  flex-wrap: wrap;
	  &-handler{
	    width: 140upx;
	    height: 140upx;
			border: 1px solid #f7f7f7;
			margin-top: 4upx;
	    border-radius: 6px;
	    background-color: #f7f7f7;
	    display: flex;
	    justify-content: center;
	    align-items: center;
	    font-size: 32px;
	    color: #999;
	    margin-right: 20upx;
	    line-height: 1;
	    &:active{
	      opacity: 0.6;
	    }
	  }
	  
	  &-images{
	    width: 140upx;
	    height: 140upx;
	    border-radius: 6px;
	    position: relative;
	    &-delete{
	      position: absolute;
	      width: 54upx;
	      height: 54upx;
	      background-color: rgba(0,0,0,0.6);
	      display: flex;
	      justify-content: center;
	      align-items: center;
	      color: #fff;
	      z-index: 3;
	      top: 0;
	      right: 0;
	      border-radius: 0 6px 0 6px;
	      &:active{
	        opacity: 0.7;
	      }
	    }
	    &-item{
	      display: flex;
	      position: relative;
	      margin-right: 20upx;
	    }
	  }
	}
</style>
